$(function () {
    vm.query();
});

/**
 * 加载订单产品数据
 * @param orderNo
 */
function loadOrderItemList(orderNo) {
    $("#orderItemTable").jqGrid({
        url: baseURL + 'production/erporderitem/erpOrderItemList',
        datatype: "json",
        postData:{
            'orderNo': orderNo
        },
        colModel: [
            { label: '产品编号', name: 'productId', index: 'product_id', width: 80, key: true , align:'center'},
            { label: '产品名称', name: 'name', index: 'name', width: 120 , align:'center'},
            { label: '克重', name: 'weight', index: 'weight', width: 80 , align:'center'},
            { label: '尺寸', name: 'addon', index: 'addon', width: 80 , align:'center'},
            { label: '单位', name: 'unit', index: 'unit', width: 80 , align:'center'},
            { label: '价格', name: 'price', index: 'price', width: 80 , align:'center'},
            { label: '份数', name: 'copy', index: 'copy', width: 80 , align:'center'},
            { label: '数量', name: 'nums', index: 'nums', width: 80 , align:'center'},
            { label: '总价', name: 'amount', index: 'amount', width: 80 , align:'center'}
        ],
        viewrecords: true,
        height: window.innerHeight * 0.2,
        rownumbers: true,
        rownumWidth: 40,
        autowidth:true,
        jsonReader : {
            root: "list",
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#orderItemTable").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    // 显示序号
    $("#orderItemTable").jqGrid('setLabel',0, '序号', '{width:80}');
}

/**
 * 查询订单商品列表
 */
function queryOrderItemList(orderNo) {
    $("#orderItemTable").jqGrid('clearGridData'); //清空表格
    $("#orderItemTable").jqGrid('setGridParam',{
        postData:{
            'orderNo': orderNo,
        },
    }).trigger("reloadGrid");
}

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        /*当前页应收总额 */
        totalAmount: 0,
        /*合计应收总额 */
        allTotalAmount:0,
        /* 实体类 */
        orderItemList: [],
        /* 枚举 */
        e:{
            productionStatusEnum: [],
        },
        /* 查询 */
        q:{
            orderNo: '',
            memberName: '',
            productionStatus: '',
            payStatus:'',
            beginCreatetime: '',
            endCreatetime: '',
        },
        /* 确认收款*/
        collectMoneyFormVisible: false,
        formLabelWidth: '120px',
        collectMoneyForm: {
            memberName: '',
            payment: '',
            payed: '',
            payableAmount: '',
            orderNo: []
        }
    },
    methods: {
        /* 订单起始 */
        checkBeginDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false, onpicking:function (dp) {
                    vm.q.beginCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /* 订单结束 */
        checkEndDate: function () {
            WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:false, onpicking:function (dp) {
                    vm.q.endCreatetime = dp.cal.getNewDateStr();
                }})
        },
        /* 获取当前日期 */
        dateFormat:function(date) {
            if (typeof date == 'string'){
                date = new Date(date);
            }
            var year=date.getFullYear();
            var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
            var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
            var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
            var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
            var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
            return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        },
        reset: function () {
            vm.q.orderNo = '';
            vm.q.memberName = '';
            vm.q.productionStatus = '';
            vm.q.beginCreatetime = '';
            vm.q.endCreatetime = '';
            vm.q.payStatus='';
            vm.reload();
        },
        collectMoney: function () {
            var orderNos = getSelectedRows();
            if(orderNos == null){
                return ;
            }

            var rowData, memberName = '';
            for (var i = 0; i < orderNos.length; i++){
                rowData = $('#jqGrid').jqGrid('getRowData', orderNos[i]);
                if (rowData.productionStatus != '生产完成'){
                    alert("请选择生产完成的订单");
                    return false;
                }
                if (isBlank(memberName)){
                    memberName = rowData.memberName;
                }else if (rowData.memberName != memberName){
                    alert("请选择同一会员用户的订单");
                    return false;
                }
            }
            vm.collectMoneyForm.memberName = memberName;
            vm.collectMoneyForm.orderNo = orderNos;

            $.ajax({
                type: "POST",
                url: baseURL + "order/monthlyorder/collectMoneyPage",
                contentType: "application/json",
                data: JSON.stringify(orderNos),
                success: function(r){
                    if(r.code == 0){
                        vm.collectMoneyForm.totalAmount = r.payableAmount;
                        vm.collectMoneyForm.payed = r.payableAmount;
                        vm.collectMoneyFormVisible = true;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        orderPay: function () {
            var payment = vm.collectMoneyForm.payment;
            if (isBlank(payment)){
                alert("收款方式不能为空");
                return false;
            }
            var postData = {
                orderNo: vm.collectMoneyForm.orderNo,
                payment: payment,
                payed: vm.collectMoneyForm.payed,
            }
            $.ajax({
                type: "POST",
                url: baseURL + "order/monthlyorder/orderPay",
                contentType: "application/json",
                data: JSON.stringify(postData),
                success: function(r){
                    if(r.code == 0){
                        vm.reload();
                        vm.collectMoneyFormVisible = false;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
         query: function () {
             $.jgrid.gridUnload("#jqGrid");
                /* 订单列表 */
                $("#jqGrid").jqGrid({
                    url: baseURL + 'order/monthlyorder/list',
                    datatype: "json",
                    colModel: [
                        { label: '订单编号', name: 'orderNo', index: 'order_no', width: 130, key: true , align:'center'},
                        { label: '文件', name: '', index: '', width: 60 , align:'center',
                            formatter: function (value, options, row) {
                                return '<a href="javascript:;">上传</a>'
                            }},
                        { label: '会员用户', name: 'memberName', index: '', width: 100 , align:'center'},
                        { label: '订单总额', name: 'totalAmount', index: 'total_amount', width: 70 , align:'center'},
                        { label: '应支付额', name: 'payableAmount', index: 'payable_amount', width: 70 , align:'center'},
                        { label: '已付金额', name: 'payed', index: 'payed', width: 70 , align:'center'},
                        { label: '优惠金额', name: 'discount', index: 'discount', width: 70 , align:'center'},
                        { label: '生产状态', name: 'productionStatus', index: 'production_status', width: 70 , align:'center',
                            formatter: function (value, options, row) {
                                var valueText = "";
                                var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
                                $.each(userData.productionStatusEnum, function (index, module) {
                                    if (module.id == value) {
                                        valueText = module.describe;
                                        return false;
                                    }
                                });
                                return valueText;
                            }
                        },
                        { label: '接单人', name: '', index: '', width: 80 , align:'center'},
                        { label: '下单时间', name: 'createTime', index: 'create_time', width: 120 , align:'center'},
                        { label: '收银时间', name: 'paymentTime', index: 'payment_time', width: 120 , align:'center'},
                        { label: '完工时间', name: 'finishTime', index: 'finish_time', width: 120 , align:'center'},
                        { label: '联系人', name: 'shipName', index: 'ship_name', width: 80 , align:'center'},
                    ],
                    viewrecords: true,
                    height: window.innerHeight * 0.55,
                    rowNum: 20,
                    rowList : [20,40,60],
                    rownumbers: true,
                    rownumWidth: 40,
                    autowidth:true,
                    multiselect: true,
                    pager: "#jqGridPager",
                    jsonReader : {
                        root: "page.list",
                        page: "page.currPage",
                        total: "page.totalPage",
                        records: "page.totalCount",
                        userdata: "userData"
                    },
                    postData:{
                        'orderNo': vm.q.orderNo,
                        'memberName': vm.q.memberName,
                        'productionStatus': vm.q.productionStatus,
                        'beginCreatetime': vm.q.beginCreatetime,
                        'endCreatetime': vm.q.endCreatetime,
                        'payStatus':vm.q.payStatus
                    },
                    prmNames : {
                        page:"page",
                        rows:"limit",
                        order: "order"
                    },
                    onCellSelect: function (rowid, iCol, cellcontent, e) { // 当点击单元格时触发
                        var row = $("#jqGrid").jqGrid("getRowData", rowid);
                        queryOrderItemList(row.orderNo);
                    },
                    loadComplete: function(data){ // 完成服务器请求后，回调函数
                        var orderNos = $('#jqGrid').getDataIDs();
                        if (orderNos.length > 0){
                            var orderNo = orderNos[0];
                            $("#jqGrid").jqGrid("setSelection", orderNo);
                            loadOrderItemList(orderNo);
                        }else {
                            $("#orderItemTable").jqGrid('clearGridData'); //清空表格
                        }
                    },
                    gridComplete:function(){
                        //隐藏grid底部滚动条
                        $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });

                        var userData = $("#jqGrid").jqGrid('getGridParam', 'userData');
                        vm.totalAmount = userData.totalAmount;
                        vm.allTotalAmount = userData.allTotalAmount;
                        vm.e.productionStatusEnum = userData.productionStatusEnum;
                    }
                });
                // 显示序号
                $("#jqGrid").jqGrid('setLabel',0, '序号', '{width:80}');
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                page:page,
                postData:{
                    'orderNo': vm.q.orderNo,
                    'memberName': vm.q.memberName,
                    'productionStatus': vm.q.productionStatus,
                    'beginCreatetime': vm.q.beginCreatetime,
                    'endCreatetime': vm.q.endCreatetime,
                    'payStatus':vm.q.payStatus
                },
            }).trigger("reloadGrid");
        },
    }
});

